<%--
  Created by IntelliJ IDEA.
  User: cxy
  Date: 2018-06-14
  Time: 15:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!-- ECharts单文件引入 -->
    <script src="${pageContext.request.contextPath}/statics/echarts.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/jquery-3.2.1.min.js"></script>
</head>
<body>

<div id="main" style="width: 1200px;height: 650px;margin: 0 auto">

</div>

<script type="text/javascript">
    var myChart = echarts.init(document.getElementById('main'));
    // 显示标题，图例和空的坐标轴
    var app = {};
    option = null;
    var posList = [
        'left', 'right', 'top', 'bottom',
        'inside',
        'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
        'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
    ];
    app.configParameters = {
        rotate: {
            min: -90,
            max: 90
        },
        align: {
            options: {
                left: 'left',
                center: 'center',
                right: 'right'
            }
        },
        verticalAlign: {
            options: {
                top: 'top',
                middle: 'middle',
                bottom: 'bottom'
            }
        },
        position: {
            options: echarts.util.reduce(posList, function (map, pos) {
                map[pos] = pos;
                return map;
            }, {})
        },
        distance: {
            min: 0,
            max: 100
        }
    };
    app.config = {
        rotate: 90,
        align: 'left',
        verticalAlign: 'middle',
        position: 'insideBottom',
        distance: 15,
        onChange: function () {
            var labelOption = {
                normal: {
                    rotate: app.config.rotate,
                    align: app.config.align,
                    verticalAlign: app.config.verticalAlign,
                    position: app.config.position,
                    distance: app.config.distance
                }
            };
            myChart.setOption({
                series: [{
                    label: labelOption
                }, {
                    label: labelOption
                }, {
                    label: labelOption
                }, {
                    label: labelOption
                }]
            });
        }
    };
    var labelOption = {

        normal: {
            show: true,
            position: app.config.position,
            distance: app.config.distance,
            align: app.config.align,
            verticalAlign: app.config.verticalAlign,
            rotate: app.config.rotate,
            formatter: '{c}  {name|{a}}',
            fontSize: 16,
            rich: {
                name: {
                    textBorderColor: '#fff'
                }
            }
        }
    };

    option = {
        title: {
            text: '销售额统计图'
        },
        color: ['#003366', '#006699', '#4cabce', '#e5323e'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {
            data: ['利润', '总进价', '营业额']
        },
        toolbox: {
            show: true,
            orient: 'vertical',
            left: 'right',
            top: 'center',
            feature: {
                mark: {show: true},
                dataView: {show: true, readOnly: false},
                magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        calculable: true,
        xAxis: [
            {
                type: 'category',
                axisTick: {show: false},
                data: []
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '利润',
                type: 'bar',
                barGap: 0,
                label: labelOption,
                data: []
            },
            {
                name: '总进价',
                type: 'bar',
                label: labelOption,
                data: []
            },
            {
                name: '营业额',
                type: 'bar',
                label: labelOption,
                data: []
            }
        ]
    };

    myChart.showLoading();    //数据加载完之前先显示一段简单的loading动画

    var month=[];    //类别数组（实际用来盛放X轴坐标值）
    var gain=[];    //销量数组（实际用来盛放Y坐标值）
    var totalInprice=[];
    var totalOutprice=[];
    $.ajax({
        type : "post",
        async : true,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
        url : "${pageContext.request.contextPath}/Census/query",    //请求发送到TestServlet处
        data : {},
        dataType : "json",        //返回数据形式为json
        success : function(result) {
            //请求成功时执行该函数内容，result即为服务器返回的json对象
            if (result) {
                for(var i=0;i<result.length;i++){
                    month.push(result[i].month);//挨个取出类别并填入类别数组
                }
                for(var i=0;i<result.length;i++){
                    gain.push(result[i].gain);//挨个取出销量并填入销量数组
                }
                for(var i=0;i<result.length;i++){
                    totalInprice.push(result[i].totalInprice);//挨个取出销量并填入销量数组
                }
                for(var i=0;i<result.length;i++){
                    totalOutprice.push(result[i].totalOutprice);//挨个取出销量并填入销量数组
                }
                myChart.hideLoading();    //隐藏加载动画
                myChart.setOption({        //加载数据图表
                    xAxis: {
                        data:month
                    },
                    series: [{
                        // 根据名字对应到相应的系列
                        name: '利润',
                        data:gain
                    },{
                        name:'总进价',
                        data:totalInprice
                    },{
                        name:'营业额',
                        data:totalOutprice
                    }]
                });

            }

        },
        error : function(errorMsg) {
            //请求失败时执行该函数
            alert("图表请求数据失败!");
            myChart.hideLoading();
        }
    })
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }

</script>





</body>


</html>
